/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.onedec.message.controller;

import com.onedec.message.entity.OnedecEmp;
import com.onedec.message.entity.OnedecFriendGroup;
import com.onedec.message.entity.OnedecMsgGroup;
import com.onedec.message.service.EmpService;
import com.onedec.message.service.FriendService;
import com.onedec.message.service.MsgGroupService;
import com.onedec.message.websocket.OnMessageWebSocket;
import com.onedec.utils.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 31014
 * @date 2020/7/13 10:44
 */
@RestController
@RequestMapping("message")
public class MessageController {
    @Resource
    private EmpService empService;
    @Resource
    private FriendService friendService;
    @Resource
    private MsgGroupService msgGroupService;

    @GetMapping("mine")
    /**
     * 获取我的信息
     */
    public ResponseEntity<OnedecEmp> getMine(Integer id) {
        final OnedecEmp emp = empService.SelectEmpById(id);
        return new ResponseEntity<OnedecEmp>(emp);
    }

    @GetMapping("friend")
    /**
     * 获取好友列表
     */
    public ResponseEntity<List<OnedecFriendGroup>> getFriends(Integer id) {
        final List<OnedecFriendGroup> groups = friendService.selectFriend(id);
        return new ResponseEntity<List<OnedecFriendGroup>>(groups);
    }

    /**
     * 获取群组列表
     *
     * @return
     */
    @GetMapping("groups")
    public ResponseEntity<List<OnedecMsgGroup>> getGroups(Integer id) {
        final List<OnedecMsgGroup> groups = msgGroupService.selectGroupByEmp(id);
        return new ResponseEntity<List<OnedecMsgGroup>>(groups);
    }

    @GetMapping("groupEmps")
    public ResponseEntity getGroupEmps(Integer id) {

        final List<OnedecEmp> emps = empService.selectGroupEmp(id);
        final HashMap<String, Object> map = new HashMap<>();
        final ArrayList<Map<String, Object>> data = new ArrayList<>();
        emps.forEach(e -> {
            final HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("username", e.getEmpName());
            hashMap.put("id", e.getEmpId());
            hashMap.put("avatar", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595245597797&di=69666dd1042f1849f904ea7d4331b495&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201805%2F30%2F20180530083702_HyrTS.thumb.700_0.jpeg");
            hashMap.put("sign", e.getEmpBak1());
            data.add(hashMap);
        });
        map.put("list", data);
        final ResponseEntity<HashMap<String, Object>> hashMapResponseEntity = new ResponseEntity<>(map);
        hashMapResponseEntity.setCode("0");
        return hashMapResponseEntity;
    }

    @PostMapping("addfriend")
    public ResponseEntity insertFriend(Integer id, Integer group) {
        friendService.insertFriend(id, group);
        return ResponseEntity.SUCCESS;
    }
}